﻿using System;
using System.Collections.Generic;

namespace Lottery.App.BLL
{
    /// <summary>
    /// 追号投注计算器
    /// </summary>
    public class InvestManager
    {
        /// <summary>
        /// 用于绑定GridView数据源
        /// </summary>
        public class Investment
        {
            /// <summary>
            /// 追号期数
            /// </summary>
            public int ID { get; set; }

            /// <summary>
            /// 当期倍数
            /// </summary>
            public int Redouble { get; set; }

            /// <summary>
            /// 当期投入
            /// </summary>
            public string SubTotal { get; set; }

            /// <summary>
            /// 总投入
            /// </summary>
            public string Total { get; set; }

            /// <summary>
            /// 当期奖金
            /// </summary>
            public string Bonus { get; set; }

            /// <summary>
            /// 盈利
            /// </summary>
            public string Profit { get; set; }

            /// <summary>
            /// 盈利率
            /// </summary>
            public string Profitability { get; set; }
        }

        private int _money, _bonus, _redouble, _count, _profit, _totalMoney;

        /// <summary>
        /// Initializes a new instance of the <see cref="InvestManager"/> class.
        /// </summary>
        /// <param name="money">投注基数金额</param>
        /// <param name="bonus">单注奖金</param>
        /// <param name="count">追号期数</param>
        /// <param name="redouble">起始倍数</param>
        /// <param name="profit">保底利润</param>
        public InvestManager(int money, int bonus, int count, int redouble, int profit)
        {
            _money = money;
            _bonus = bonus;
            _count = count;
            _redouble = redouble;
            _profit = profit;
        }

        public List<Investment> GetResult()
        {
            List<Investment> list = new List<Investment>();

            for (int i = 1; i <= _count; i++)
            {
                list.Add(Calc(i));
            }

            return list;
        }

        private Investment Calc(int num)
        {
            int bs = _redouble;
            while ( bs * _bonus < _totalMoney + _money * bs + _profit)
                bs++;

            _redouble = bs;

            int money = _money * bs;
            _totalMoney += money;
            int bonus = _bonus * bs;
            float profit = bonus - _totalMoney; 

            Investment h = new Investment();
            h.ID = num;
            h.Redouble =  bs;
            h.SubTotal = String.Format("{0:C0}", money);
            h.Total = String.Format("{0:C0}", _totalMoney);
            h.Bonus = String.Format("{0:C0}", bonus);
            h.Profit = String.Format("{0:C0}", profit);
            h.Profitability = String.Format("{0:P}", profit / _totalMoney);

            return h;
        }
        
    }
}
